import { defineStore } from 'pinia';

// 定义存储的类型
interface User {
  stuId: string;
  stuClass: string;
  // 可以添加其他用户相关信息
}

// 从 Local Storage 中获取用户数据
function getUserFromLocalStorage(): User | null {
  const userData = localStorage.getItem('user');
  return userData ? JSON.parse(userData) : null;
  
}

// 创建并导出使用 Pinia 的 store
export const useAuthStore = defineStore('auth', {
  state: () => {
    return {
      duration:600,
      up: false,
      user: getUserFromLocalStorage(),
      background:{
        height:600,
        width:500,
        color:"#636363",
      }
    };
  },
  actions: {
    setbackground(x:any){
      this.background=x;
    },
    setduration(x:number){
      this.duration=x;
    },
    setuptrue(){
      this.up=true;
    },
    setupfalse(){
      this.up=false;
    },
    setuser(value: User) {
      this.user = value;
      localStorage.setItem('user', JSON.stringify(value));
      console.log(this.user);
    },
    logout() {
      this.user = null;
      localStorage.removeItem('user');
    },
  },
});
